package com.bit.springbook.config;

import com.bit.springbook.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
//@ControllerAdvice // 这个默认是返回视图 如果要返回数据必须加 @ResponseBody
//@ResponseBody
@RestControllerAdvice // 等于 @ResponseBody + @ControllerAdvice
public class ErrorActive {
    @ExceptionHandler
    public Result handle(Exception e) {
        log.error("发生异常: " + e);
        return Result.fail("发生异常" + e.getMessage());
    }

    @ExceptionHandler
    public Result handle(NullPointerException e) {
        log.error("发生空指针异常: " + e);
        return Result.fail("发生空指针异常" + e.getMessage());
    }

    @ExceptionHandler
    public Result handle(ArithmeticException e) {
        log.error("发生算数异常: " + e);
        return Result.fail("发生算数异常" + e.getMessage());
    }
}
